﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CJ_Logging.Configuration.Config;
using CJ_Logging.Configuration;
using System.Net;
using System.Collections;

namespace CJ_Logging
{
    public class CatchException
    {
        private static CatchException m_CatchException = null;

        public static CatchException Instance
        {
            get {
                if (m_CatchException == null)
                    m_CatchException = new CatchException();
                return m_CatchException;
            }
        }


        /// <summary>
        /// 处理异常
        /// </summary>
        /// <param name="error"></param>
        public void Save(string error)
        {
            this.Save("",error);
        }

        /// <summary>
        /// 处理异常
        /// </summary>
        /// <param name="error"></param>
        public void Save(string title,string error)
        {
            try
            {
                SaveExceptionInfoConfig config = LoadConfig.Instance.LoadSaveExceptionInfoConfig();
                if (config.IsWriteFile) //是否写日志
                    LogHelper.Instance.WriteError(error);
                if (config.IsSendMail)  //是否发邮件
                {
                    //加入日期
                    error = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ms") + "\r\n" + error;
                    SendMail.Instance.SendMailBySmtp(title, error);
                }
            }
            catch { }
        }

        public string GetExceptionDesc(Exception exception)
        {
            StringBuilder sbError = new StringBuilder();

            string hostName = Dns.GetHostName(); //得到本机的主机名
            //取得本机IP 
            IPHostEntry ipEntry = Dns.GetHostByName(hostName);
            string ipAddress = ipEntry.AddressList[0].ToString();
            sbError.Append("[主机名]：");
            sbError.Append(hostName);
            sbError.Append("\r\n[主机IP]：");
            sbError.Append(ipAddress);
            sbError.Append("\r\n");

            if (exception.Data != null)
            {
                foreach (DictionaryEntry item in exception.Data)
                {
                    sbError.Append("[");
                    sbError.Append(item.Key);
                    sbError.Append("]：");
                    sbError.Append(item.Value);
                    sbError.Append("\r\n");
                }
            }

            sbError.Append("\r\n");
            sbError.Append("出现应用程序未处理的异常：");
            sbError.Append("\r\n");
            string str =  string.Empty;
            if (exception != null)
            {
                str = string.Format("异常类型：{0}\r\n异常消息：{1}\r\n异常信息：{2}\r\n",
                     exception.GetType().Name, exception.Message, exception.StackTrace);
            }
            else
            {
                str = string.Format("应用程序线程错误:{0}", exception);
            }
            sbError.Append(str);
            
            return sbError.ToString();
        }
    }
}
